﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_Inspector
	{
		private class MyMessage : Message
		{
		}
		private class MyInformation : Information
		{
		}
		private class MyOperation : Operation<String, Int32>
		{
		}

		private abstract class TestAbstractClass
		{
			[MessageHandler, ExclusiveExecution]
			protected abstract void TestMethod3(MyMessage message);

			[InformationHandler]
			private static void TestMethod5(MyInformation information)
			{
			}

			[InformationHandler]
			private void TestMethod9(Int32 index, String text)
			{
			}

			[OperationHandler]
			protected abstract Task<String> TestMethod10(MyOperation operation);
		}

		private class TestBaseClass : TestAbstractClass
		{
			[MessageHandler, ConcurrentExecution]
			private void TestMethod2(MyMessage message)
			{
			}

			[NoParallelismAttribute]
			protected override void TestMethod3(MyMessage message)
			{
			}

			[InformationHandler, MaximumParallelismAttribute, IndependentExecution]
			private void TestMethod4(MyInformation information)
			{
			}

			[InformationHandler]
			private void TestMethod8(Int32 index)
			{
			}

			protected override Task<String> TestMethod10(MyOperation operation)
			{
				throw new NotImplementedException();
			}
		}

		private class TestClass : TestBaseClass
		{
			[MessageHandler]
			private void TestMethod1(MyMessage message)
			{
			}

			[OperationHandler]
			public String TestMethod6(MyOperation operation)
			{
				return "done";
			}

			[OperationHandler]
			public None TestMethod7(MyOperation operation)
			{
				return None.Instance;
			}

			protected async override Task<String> TestMethod10(MyOperation operation)
			{
				await Task.Factory.StartNew(() => Thread.Sleep(250));
				return "done";
			}

			[MessageHandler]
			private async Task TestMethod11(MyMessage message)
			{
				await Task.Factory.StartNew(() => Thread.Sleep(250));
			}

			[InformationHandler]
			private async Task TestMethod12(MyInformation message)
			{
				await Task.Factory.StartNew(() => Thread.Sleep(250));
			}
		}

		private HandlerInfo FindHandlerInfo(HandlerInfo[] handlerInfos, String methodName)
		{
			return handlerInfos.SingleOrDefault(h => h.MethodInfo.Name.Equals(methodName));
		}


		// --- TESTS ---


		[Test]
		public void Test_Inspect()
		{
			var handlerInfos = Inspector.Inspect(typeof(TestClass));
			var handlerInfos2 = Inspector.Inspect(typeof(TestClass));

			Assert.That(handlerInfos2, Is.SameAs(handlerInfos));

			var handler1 = FindHandlerInfo(handlerInfos, "TestMethod1");
			Assert.That(handler1, Is.Not.Null);
			Assert.That(handler1.MessageType, Is.EqualTo(typeof(MyMessage)));
			Assert.That(handler1.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handler1.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));

			var handler2 = FindHandlerInfo(handlerInfos, "TestMethod2");
			Assert.That(handler2, Is.Not.Null);
			Assert.That(handler2.MessageType, Is.EqualTo(typeof(MyMessage)));
			Assert.That(handler2.ExecutionMode, Is.EqualTo(ExecutionMode.Concurrent));
			Assert.That(handler2.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));

			var handler3 = FindHandlerInfo(handlerInfos, "TestMethod3");
			Assert.That(handler3, Is.Not.Null);
			Assert.That(handler3.MessageType, Is.EqualTo(typeof(MyMessage)));
			Assert.That(handler3.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handler3.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));

			var handler4 = FindHandlerInfo(handlerInfos, "TestMethod4");
			Assert.That(handler4, Is.Not.Null);
			Assert.That(handler4.MessageType, Is.EqualTo(typeof(MyInformation)));
			Assert.That(handler4.ExecutionMode, Is.EqualTo(ExecutionMode.Independent));
			Assert.That(handler4.ParallelismLevel, Is.EqualTo(ParallelismLevel.MaximumParallelism));

			var handler5 = FindHandlerInfo(handlerInfos, "TestMethod5");
			Assert.That(handler5, Is.Not.Null);
			Assert.That(handler5.MessageType, Is.EqualTo(typeof(MyInformation)));
			Assert.That(handler5.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handler5.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));

			var handler6 = FindHandlerInfo(handlerInfos, "TestMethod6");
			Assert.That(handler6, Is.Not.Null);
			Assert.That(handler6.MessageType, Is.EqualTo(typeof(MyOperation)));
			Assert.That(handler6.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handler6.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));

			var handler7 = FindHandlerInfo(handlerInfos, "TestMethod7");
			Assert.That(handler7, Is.Null);

			var handler10 = FindHandlerInfo(handlerInfos, "TestMethod10");
			Assert.That(handler10, Is.Not.Null);
			Assert.That(handler10.MessageType, Is.EqualTo(typeof(MyOperation)));
			Assert.That(handler10.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handler10.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));

			var handler11 = FindHandlerInfo(handlerInfos, "TestMethod11");
			Assert.That(handler11, Is.Not.Null);
			Assert.That(handler11.MessageType, Is.EqualTo(typeof(MyMessage)));
			Assert.That(handler11.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handler11.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));

			var handler12 = FindHandlerInfo(handlerInfos, "TestMethod12");
			Assert.That(handler12, Is.Not.Null);
			Assert.That(handler12.MessageType, Is.EqualTo(typeof(MyInformation)));
			Assert.That(handler12.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handler12.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));
		}

	}
}

#endif
